<?php
namespace QnuiRings\Controller;
use Think\Controller;
// 软件名称：互动折扣王-连环折扣-满就送折扣
// 代码日期：v3.0 2018-7-14
// 作者：杨道泉
// 文件名：QnuiZhekouLimitSaveController.class.php
// 功能： 使用千牛UI发布限购折扣

class QnuiZhekouLimitSaveController extends QnuiBaseController {

    public function save_limit(){
        $dbId = I("db_id");
        $tagName=I("tagName");
        $startTime=date("Y-m-d H:i:s",I("startTime"));
        $endTime=date("Y-m-d H:i:s",I("endTime"));
        $notice=I("notice");
        $activityId = I("act_id");
        
        try{
            $PROM = D("PromotionsZhekouLimit");
            $data["sid"]=session("SHOP_ID");
            $data["activity_id"] =$activityId;
            $data["name"] = $tagName;
            $data["description"] = $notice;
            $data["start_time"] = $startTime;
            $data["end_time"] = $endTime;
            $data["is_user_tag"] = false;
            $data["tag_name"] = "";
            $data["tag_id"] = 0;
            $data["status"] = 0;
            if(!$dbId){
                $reId = $PROM->add($data);
            }else{
                $reId = $PROM->where(Array("id"=>$dbId,"sid"=>session("SHOP_ID")))->save($data);
                }
        }catch(\Exception $e){
             $msg = $e->getMessage();
         }
        $re = new \StdClass();
        if($reId){
            $re->code = 200;
            $re->id = $reId;
        }else{
            $re->code = 910;
            $re->msg = $msg;
          }
        $this->ajaxReturn($re);
    }
    public function save_items_to_database(){
        $data["activity_id"] = I("activity_id");
        $data["detail_id"] = I("detail_id");
        $data["promotion_type"] = I("promotion_type");
        $data["promotion_value"] = I("promotion_value")*100;
        $data["num_iid"] = I("num_iid");
        $data["title"] = I("title");
        $data["pic_url"] = I("pic_url");
        $data["price"] = I("price");
        $data["is_limit_buy"] = I("act_type")=="limit"?1:0;
        $data["limit_buy_count"] = I("limit_buy_count");
        $data["sid"] = session("SHOP_ID");
        try{
            $ITEMS = M("PromotionsZhekouLimitItems");
            $re = $ITEMS->add($data);
        }catch(\Exception $e){
                $msg = $e->getMessage();
          }
        $resp = new \StdClass();
        if(!$re){
            $resp->code = 910;
            $resp->is_success = false;
            $resp->msg="抱歉，添加出错。".$msg;
          }
        $this->ajaxReturn(pre_resp($resp));
    }
    public function get_list(){
        try{
            $PROM = D("PromotionsZhekouLimit");
            $where["sid"]=session("SHOP_ID");
            $data = $PROM->where($where)->getField("id,name,activity_id,start_time,end_time,description");
            $total_count = $PROM->where($where)->count();
        }catch(\Exception $e){
                $msg = $e->getMessage();
          }
        $resp = new \StdClass();
        if($msg){
            $resp->code = 910;
            $resp->is_success = false;
            $resp->msg="抱歉，添加出错。".$msg;
        }else{
            
            $resp->code = 200;
            if(count($data)>0){
                $resp->activity_list->common_item_activity = array_values($data);
                $resp->total_count = $total_count;
                }
          }
       $this->ajaxReturn($resp);
       //insert into `rings_promotions_zhekou_limit` (sid,activity_id,name,description,start_time,end_time) VALUES (64665313,6766767207,'卖家促销','设置的折扣','2018-08-28 00:00:00','2018-08-29 00:00:00')
    }
    public function remove(){
        $activity_id = I("activity_id");
        try{
            $PROM = D("PromotionsZhekouLimit");
            $where["sid"]=session("SHOP_ID");
            $where["activity_id"]=$activity_id;
            $data = $PROM->where($where)->delete();
        }catch(\Exception $e){
            $msg = $e->getMessage();
          }
        $resp = new \StdClass();
        if($msg){
            $resp->code = 910;
            $resp->is_success = false;
            $resp->msg="抱歉，删除出错。".$msg;
        }else{
            $resp->code = 200;
            $resp->is_success = true;
          }
        $this->ajaxReturn($resp);
    }
}